<template>
	<view class="content">
		<view class="baseView">
		<uni-nav-bar left-icon="back" title="确认订单" @clickLeft="gotoBack" :border="false" color="#fff" rightIcon="headphones" right-text="客服"  backgroundColor="transparent"></uni-nav-bar>	
		</view>
		<view class="viewG">
			<view class="topView">
				<view class="lab1">支付剩余时间</view>
				<view class="time">
					<uni-countdown :show-day="false" :show-hour="false" :minute="timeMM" :second="timeSS"></uni-countdown>
				</view>
				<view class="order-info">订单编号：{{orderNum}}</view>
			</view>
			<view class="payWay">
				<view class="company" v-for="(item, index) in payList" :key="index">
					<view class="comInfo">
						<image v-if="item.payType == 2" src="../../static/icon_wechat_pay.png" mode="widthFix"></image> 
						<image v-if="item.payType == 1" src="../../static/icon_ali_pay.png" mode="widthFix"></image>
					    <view class="comInfoDetail">{{item.payName}}</view>
					</view>
					
					<view class="all_select_box" @click="btnChangeBox(index)">	
						<uni-icons :type="izDefaultIndexId == index?'checkbox-filled':'checkbox'" :color="izDefaultIndexId == index?'#4CD964':'#ccc'" size="25"></uni-icons>	
					</view>
				</view>
			</view>
		</view>
		<view class="btnGroup">
			<view class="payBtn">
				<view class="total"> <text>合计:</text>￥{{orderAmount}}</view>
				<view class="btn showBtnBackColor" @click="btnAddAddr">
					确认支付
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {throttle} from '../../util/util.js'
	import {appPay} from '../../api/index.js'
	export default {
		data() {
			return {
				izDefaultIndexId:0,
				orderNum:'',
				isShowTime:false,
				timeMM:0,
				orderInfo:{},
				titleStr:'',
				timeSS:0,
				payList:[],
				requestUrl:'',
				orderAmount:'0.00'
				
			}
		},
		onLoad(option) {
			
			this.requestUrl =getApp().globalData.baseUrl+'/'
			
			if (option.result) {
				let orderInfo = JSON.parse(decodeURIComponent(option.result));
				let time= orderInfo.expireTime
				if (time >0) {
					this.timeMM = Math.floor(time / 60);
					this.timeSS = time % 60;
				}
				this.isShowTime = true
				
				if (orderInfo.orderType == 2) {
					this.titleStr='哈芙琳-零售校服下单'
				} else{
					this.titleStr='哈芙琳-预售校服下单'
				}
				
				this.orderInfo = orderInfo;
				
				this.orderAmount = parseFloat(orderInfo.totalAmount).toFixed(2)
				this.orderNum = orderInfo.code
				this.payList = orderInfo.payList
				
				}
		},
		methods: {
			//支付宝支付
			async requstAppAliPay(){
				
				let item = this.payList[this.izDefaultIndexId]
				let payType='';
				let url='';
				
				
				let totalAmount  = 0
				if (item.payType == 2) {
					totalAmount = parseFloat(this.orderInfo.totalAmount).toFixed(2);
					totalAmount = totalAmount*100;
					totalAmount = parseInt(totalAmount)
					
					payType='wxpay'
					url= this.requestUrl+item.code+'/wxPay/appPay'
				} else{
					totalAmount  = this.orderInfo.totalAmount
					payType ='alipay'
					url= this.requestUrl+item.code+'/aliPay/appPay'
					
					
					//这个是后来加的要删掉
					let  dict  = {storeCode:item.code,channelId:item.channelId,orderCode:this.orderNum,orderTitle:this.titleStr,body:this.orderInfo.title,orderTotalAmount:totalAmount,orderSubject:this.orderInfo.orderSubject,timeoutExpress:this.orderInfo.timeoutExpress};
					 
					let {data} = await appPay(dict,url);
					console.log('pay',dict,url,data,payType);
					// DEMO
					var that = this
					// 获取jyAlipay对象
					const jyAliPay = uni.requireNativePlugin('JY-ALIPAY')
					jyAliPay.show({ // 发起支付
					    if_sanbox:true, 
					    auto_create_order_info:false,
					    appid:'',
					    rsa2_private:"",
					    order_info:data
					}, function (result) { // 支付完成回调
					    console.log(result)
					     if(result.resultStatus == '5002555')
					     {
					         uni.showToast({
					         	icon:'none',
								title:'支付sdk出错'
					         })
					     }else if(result.resultStatus == '9000')
					     {
					   //       uni.showToast({
					   //       	icon:'none',
								// title:'支付成功'
					   //       })
					   
					     uni.navigateTo({
					     	url:'../paySuccess/paySuccess?payType='+payType+'&orderCode='+dict.orderCode+'&orderTotalAmount='+that.orderAmount+'&orderTime='+that.orderInfo.orderTime
					     })
					   
					     }else{
							 uni.showToast({
							 	icon:'none',
								title:result.memo
							 })
					         // that.vusui.msg()
					     } 
					});
					
					return
					
					
				}
				
				
				
				let  dict  = {storeCode:item.code,channelId:item.channelId,orderCode:this.orderNum,orderTitle:this.titleStr,body:this.orderInfo.title,orderTotalAmount:totalAmount,orderSubject:this.orderInfo.orderSubject,timeoutExpress:this.orderInfo.timeoutExpress};
				 
				let {data} = await appPay(dict,url);
				
				// uni.navigateTo({
				// 	url:'../paySuccess/paySuccess?payType='+payType+'&orderCode='+dict.orderCode+'&orderTotalAmount='+this.orderAmount+'&orderTime='+this.orderInfo.orderTime
				// })
				
				console.log('pay',dict,url,data,payType);
			    uni.requestPayment({
			        provider:payType,
			        orderInfo: data, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
			        success: (res) =>{
			            console.log('success:' + JSON.stringify(res));
						// uni.showToast({
						// 	title: '支付成功',
						// 	icon: 'none'
						// })
						
						uni.navigateTo({
							url:'../paySuccess/paySuccess?payType='+payType+'&orderCode='+dict.orderCode+'&orderTotalAmount='+this.orderAmount+'&orderTime='+this.orderInfo.orderTime
						})
						
			        },
			        fail: function (err) {
			            console.log('fail:' + JSON.stringify(err));
						
						uni.showToast({
							title: JSON.stringify(err),
							icon: 'none'
						})
			        }
			    });
			},
			btnChangeBox(index){
				this.izDefaultIndexId = index
			},
			gotoBack(){
				uni.navigateBack({
					delta:1
				})
			},
			//支付
			btnAddAddr:throttle(function(){
				if (this.payList.length == 0) {
					
                        uni.showToast({
							title: '支付异常，请重新尝试',
							icon: 'none'
						})
						return
				}

				this.requstAppAliPay()
		
			},1500)
		}
	}
</script>

<style>
    page{
		background-color: #f2f2f2;
		height: 100%;
	}
	.baseView{
			padding-top:  var(--status-bar-height);
			background: linear-gradient(180deg, #8250FD 0%, #5644E5 100%);
			height: 400rpx;
			
	}
	.viewG{
		position: relative;
		top: -250rpx;
	}
	.topView{
		
		margin: 0 20rpx;
		background-color: #fff;
		padding-top: 60rpx;
		padding-bottom: 100rpx;
		border-radius: 16rpx;
		
		
	}
	
	.lab1 {
		
		font-size: 34upx;
		font-weight: 500;
		color: #333;
		text-align: center;
	
	}
	.time{
		color: #333;
		text-align: center;
		font-size: 40rpx;
		margin: 20rpx auto 0;
		font-weight: bold;
		width: 100rpx;
		
	}
	.order-info {
		
		font-size: 28upx;
		font-weight: 500;
		color: #ccc;
		text-align: center;
		margin-top: 20rpx;
		
		
	}
	
	.payWay{
		margin: 30rpx 20rpx 0;
		/* background-color: #fff; */
		
	}
	.company{
		display: flex;
		justify-content: space-between;
		padding:45rpx 30rpx;
		margin-bottom: 20rpx;
		background-color: #fff;
		align-items: center;
		
	
		
	}
	/* .company:last-child{
		border-top: 1rpx solid #f2f2f2;
	} */
	.comInfo{
       display: flex;
	   
	}
	.comInfo image{
		width: 50rpx;
		height: 50rpx;
	}
	.comInfo .comInfoDetail{
		font-size: 32rpx;
		font-weight: 300;
		margin-left: 10rpx;
		
	}
	.all_select_box{
		height: 50rpx;
		width: 50rpx;
	}
	.btnGroup{
	   position: fixed;
	   bottom: 0;
	   
	   padding: 0 20rpx 10rpx;
	   padding-bottom: 20rpx;
	   padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
	   padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
	   left: 0;
	   width: 710rpx;
	   background-color: #fff;
	   
	  
	}
	.payBtn{
		margin-top: 10rpx;
		display: flex;
		justify-content: space-between;	
		align-items: center;
	
	}
	.total {
		// display: flex;
		// align-items: flex-start;
		color: #F54353;
		font-size: 28rpx;
		font-weight: bold;
		
	}
	.total  text{
		color: #333;
		font-weight: 500;
		font-size: 28rpx;
	}
	.btnGroup .btn{
	   color: #fff;
	   background-color: #ff0000;
	   width: 202rpx;
	   height: 74rpx;
	   line-height: 74rpx;
	   border-radius: 37rpx;
	   font-size: 28rpx;
	   font-weight: 300;
	   text-align: center;
			   
	}
	
</style>
